window._CROF = {

    bossSkillbd:[null,'练气','筑基','结丹','元婴','化神','合体','渡劫','大乘','真仙','金仙','大魔王'],

    baseMainNpc:{
        'ren':['武剑心','风行者','庄聚贤'],
        'yao':['飞箭羽','石断岳','魅影客'],
        'xian':['梦神机','道玄尘','青阳仙','龙太子']
    },

    npcForJg:[],

    ftypeArr:['金','木','水','火','土'],

    ftypexw:{
        '金':'jin',
        '木':'mu',
        '水':'shui',
        '火':'huo',
        '土':'tu'
    },

    faceArr:null,

    faceMap:{},

    jobSkillMap:{},

    jobPathSkillMap:null,

    equipMap:null,

    //根据装备名和锻造等级构造装备
    createEquipByTypeAndDz:function(type,dz){

        var tempequip = _IOF.createEquip(type,null);
        if(dz&&dz>0){
            var sourceAttr = _CU.cloneObj(tempequip.attr);
            for(var key in tempequip.attr){
                if(_ROLEDATA._mainAttrConfig[key]){
                    var tempsourceAttr = sourceAttr[key];
                    tempequip.attr[key] = Math.floor(tempsourceAttr*(1+_MDZ._DZPER*dz));
                }
            }
            tempequip.sourceAttr = sourceAttr;
            tempequip.dzlevel = dz;
        }
        return tempequip;
    },

    //根据职业取装备
    getEquipByJob:function(job){
        var jobConfig = _JOB[job];
        if(jobConfig&&jobConfig.equipType){
            var equiptMap = _CROF.getEquipMap();
            var resultMap = {};
            for(var i=0;i<jobConfig.equipType.length;i++){
                var etype = jobConfig.equipType[i];
                var earr = equiptMap[etype];
                for(var j=0;j<earr.length;j++){
                    //console.log('修为匹配:',job,etype,jobConfig.xw,earr[j].xw,XWH.isXwEnough(earr[j],jobConfig));
                    if(XWH.isXwEnough(earr[j],jobConfig)){
                        if(!resultMap[earr[j].name]){
                            resultMap[earr[j].name]=earr[j];
                            break;
                        }
                    }
                }
            }
            var resultArr = [];
            for(var key in resultMap){
                resultArr.push(key);
            }
            return resultArr;
        }
        return null;
    },

    //根据职业取装备
    getEquipByJobAndRole:function(job,role){
        var jobConfig = _JOB[job];
        if(jobConfig&&jobConfig.equipType){
            var equiptMap = _CROF.getEquipMap();
            var resultArr = [];
            for(var i=0;i<jobConfig.equipType.length;i++){
                var etype = jobConfig.equipType[i];
                var earr = equiptMap[etype];
                if(earr!=null&&earr.length>0){
                for(var j=0;j<earr.length;j++){
                    //console.log('修为匹配:',job,etype,jobConfig.xw,earr[j].xw,XWH.isXwEnough(earr[j],jobConfig));
                    if(XWH.isXwEnough(earr[j],role)){
                        resultArr.push(earr[j].name);
                        break;
                    }
                }
                }
            }
            return resultArr;
        }
        return null;
    },

    //按类型构建装备map
    getEquipMap:function(){

        if(!_CROF.equipMap){
            _CROF.equipMap={};
            for(var key in _EQUIP){
                if(!_CROF.equipMap[_EQUIP[key].type]){
                    _CROF.equipMap[_EQUIP[key].type]=[];
                }
                _CROF.equipMap[_EQUIP[key].type].push({name:key,qua:_EQUIP[key].qua,xw:_CU.cloneObj(_EQUIP[key].xw)});
            }

            for(var key in _CROF.equipMap){
                _CROF.equipMap[key].sort((a,b)=>{
                    /*
                    if(
                        _CS.qua[a.qua]
                        &&_CS.qua[b.qua]
                    ){
                        return _CS.qua[b.qua].val-_CS.qua[a.qua].val;
                    }*/

                    var maxa = 0;
                    var maxb = 0;
                    for(var txw in a.xw){
                        if(a.xw[txw]>maxa){
                            maxa = a.xw[txw];
                        }
                    }

                    for(var txw in b.xw){
                        if(b.xw[txw]>maxb){
                            maxb = b.xw[txw];
                        }
                    }

                    return maxb-maxa;
                });
            }
        }

        return _CROF.equipMap;
    },

    getFaceArr:function(){

        if(!_CROF.faceArr){

            _CROF.faceArr = [];

            for(var key in _NPC){
                var npcConf = _NPC[key];
                _CROF.faceArr.push({face:npcConf.face,pf:npcConf.pf,name:key});
            }

        }

        return _CROF.faceArr;

    },

    getRandomFaceObj:function(){
        /*
        var tempfaceArr = _CROF.getFaceArr();
        if(_CROF.faceIndex){
            _CROF.faceIndex++;
            if(_CROF.faceIndex>=tempfaceArr.length){
                _CROF.faceIndex=0;
            }
        }else{
            _CROF.faceIndex=1;
        }

        return tempfaceArr[_CROF.faceIndex];
        */


        
        var tempfaceArr = _CROF.getFaceArr();
        if(tempfaceArr.length>0){
            return _CU.randomObj(tempfaceArr);
        }else{
            return {face:"无业",pf:"无业"};
        }
    },


    initXw:function(npcxw,lv){

        var tempxwObj = {};

        for(var key in npcxw){
            //初始修为随机
            tempxwObj[key]=Math.floor(Math.random()*100);

            //根据修为等级和人物等级计算
            var xwlevel = npcxw[key];
            if(window._XWLEVEL[xwlevel]){
                var min = window._XWLEVEL[xwlevel][0];
                var max = window._XWLEVEL[xwlevel][1];
                tempxwObj[key]+=_CU.randomXwValue(min,max,lv);
                if(tempxwObj[key]>_CS.maxxw){
                    tempxwObj[key]=_CS.maxxw;
                }
            }
        }

        return tempxwObj;

    },

    //随机主角
    getRandomMainRole:function(isMainRole,tempnpc,race){

        var randomNpc = tempnpc;

        if(!randomNpc){
            var randomNpcArr = (isMainRole&&_CROF.baseMainNpc[race])?_CROF.baseMainNpc[race]:_MJG.getNpcForJg();
            if(randomNpcArr.length<=0){
                return null;
            }
            randomNpc = _CU.randomObj(randomNpcArr);
        }

        var randomNpcConfig = _NPC[randomNpc];
        var basevalue = 100;
        if(randomNpcConfig.race=="xian"){
            basevalue=300;
        }else if(randomNpcConfig.race=="yao"){
            basevalue=200;
        }

        var randomftype = randomNpcConfig.ftype;
        var tempxwObj = _CROF.initXw(randomNpcConfig.npcxw,0);

        var randomRole = {
            name:_NAMECREATOR.getRandomName(),
            face:randomNpcConfig.face,
            //job:randomNpcConfig.job,
            pf:randomNpcConfig.pf,
            ftype:randomftype,
            npctype:randomNpc,
            //race:randomNpcConfig.race,
            exp:0,
            hp:0,
            mg:0,
            plv:Math.floor(Math.random()*basevalue)+1,
            dlv:Math.floor(Math.random()*basevalue)+1,
            ilv:Math.floor(Math.random()*basevalue)+1,
            mlv:Math.floor(Math.random()*basevalue)+1,
            hlv:Math.floor(Math.random()*basevalue)+1,
            skill:_CU.cloneObj(randomNpcConfig.skill),
            xw:tempxwObj,
            equip:[]
        };

        //if(isMainRole&&randomRole.skill&&randomRole.skill.length>0){
         //   randomRole.skill = [_CU.randomObj(randomRole.skill)];
        //}

        //_CROF.getSkillByXw(randomRole,randomRole.skill,false,true);

        var randomData = _ROLEDATA.getRoleDataOfPlayer(randomRole);

        randomRole.hp = randomData.maxHp;
        randomRole.mg = randomData.maxMagic;

        return randomRole;

    },

    getRandomRole:function(isJg,job){

        var jobConfig = _JOB[job];
        var faceObj = isJg?{face:jobConfig.face,pf:jobConfig.pf}:_CROF.getRandomFaceObj();
        var randomftype = _CU.randomObj(_CROF.ftypeArr);
        var ftypexw = _CROF.ftypexw[randomftype];
        var randomRole = {
            name:_NAMECREATOR.getRandomName(),
            face:faceObj.face,
            job:job,
            pf:faceObj.pf,
            ftype:randomftype,
            exp:0,
            hp:0,
            mg:0,
            plv:Math.floor(Math.random()*100)+1,
            dlv:Math.floor(Math.random()*100)+1,
            ilv:Math.floor(Math.random()*100)+1,
            mlv:Math.floor(Math.random()*100)+1,
            hlv:Math.floor(Math.random()*100)+1,
            skill:_CROF.getSkillOfJobPath(job),
            xw:_CU.cloneObj(jobConfig.xw)
        };

        if(!randomRole.xw[ftypexw]){
            //randomRole.xw[ftypexw]=1;
            var ftypexwvalue = 1;
            if(isJg&&jobConfig&&jobConfig.level){
                if(jobConfig.level==1){
                    ftypexwvalue=50;
                }else if(jobConfig.level==2){
                    ftypexwvalue=100;
                }else if(jobConfig.level==3){
                    ftypexwvalue=200;
                }else if(jobConfig.level==4){
                    ftypexwvalue=400;
                }else if(jobConfig.level==5){
                    ftypexwvalue=800;
                }
            }
            randomRole.xw[ftypexw]=ftypexwvalue;
        }

        _CROF.getSkillByXw(randomRole,randomRole.skill,false,true);

        var randomData = _ROLEDATA.getRoleDataOfPlayer(randomRole);

        randomRole.hp = randomData.maxHp;
        randomRole.mg = randomData.maxMagic;

        return randomRole;


    },

    //构造boss
    createNpcRole:function(npc,param){

        var npcConf = _NPC[npc];
        if(npcConf){
            var job = npcConf.job;
            var hardConf = _MCH.getHardConf();
            var isHecheng = param.isHecheng;//是否合成
            var lv = param.lv?param.lv:5;
            var dz = param.dzlevel?param.dzlevel:0;
            var chapter = param.chapter;

            var tmpnpc = _CU.cloneObj(npcConf);
                //tmpnpc.isboss = true;
                tmpnpc.npctype = npc;
            
            //技能没有配置或者技能数少于10才自动匹配
            if(!tmpnpc.skill||tmpnpc.skill.length<12){

                if(!tmpnpc.skill){
                    tmpnpc.skill = [];
                }

                //补充职业技能
                tmpnpc.skill=tmpnpc.skill.concat(_CROF.getSkillOfJobPath(job));

                //技能数超过则消减
                var maxskillnmb = _SKILLHEPLER.getRoleSkillNmb(tmpnpc);
                if(tmpnpc.skill.length>maxskillnmb){
                    tmpnpc.skill.length=maxskillnmb;
                }

            }

            tmpnpc.equip = [];

            tmpnpc.xw =  _CROF.initXw(npcConf.npcxw,lv);

            //根据修为补充技能
            _CROF.getSkillByXw(tmpnpc,tmpnpc.skill,false,false);

            //1级通配技能
            if(
                !isHecheng
                &&hardConf.askill
                &&tmpnpc.level
                &&_CROF.bossSkillbd[tmpnpc.level]
                &&_SKILLBD[_CROF.bossSkillbd[tmpnpc.level]]
            ){
                tmpnpc.skill.push(_CROF.bossSkillbd[tmpnpc.level]);
            }

            //2级通配技能
            if(
                !isHecheng
                &&hardConf.bskill
                &&chapter
                &&_SKILLBD[chapter+"守军"]
            ){
                tmpnpc.skill.push(chapter+"守军");
            }
                
           //根据职业和实际修为获取装备
            var equipArr = _CROF.getEquipByJobAndRole(job,tmpnpc);
            if(equipArr&&equipArr.length>0){
                for(var i=0;i<equipArr.length;i++){
                    tmpnpc.equip.push(_CROF.createEquipByTypeAndDz(equipArr[i],dz));
                }
            }

            tmpnpc.exp = _EXPMANAGER.getExpByLevel(lv);

            var npcdata = _ROLEDATA.getRoleDataOfPlayer(tmpnpc);
            tmpnpc.hp = npcdata.maxHp;
            tmpnpc.mg = npcdata.maxMagic;

            return tmpnpc;
        }

        return null;

    },

    //_CROF.createJobRole('无业',{})
    createJobRole:function(jobName,param){
        var jobConfig = _JOB[jobName]
        if(jobConfig&&param){

            var hardConf = _MCH.getHardConf();
            var chapter = param.chapter;
            var jobPath = param.jobPath;
            var lv = param.lv?param.lv:5;
            var dz = param.dzlevel?param.dzlevel:0;
            var attr = param.attr?param.attr:100;
            var ftype = param.ftype?param.ftype:_CU.randomObj(_CROF.ftypeArr);
            var ftypexw = _CROF.ftypexw[ftype];
            var npcname = param.npcname?param.npcname:_NAMECREATOR.getRandomName();
            var face = jobConfig.face;
            var pf = jobConfig.pf;
            var isRandom = (!param.noRandom)?true:false;
            var chapterLv = param.chapterLv;
            var jobRoleSkill = null;

            if(!jobPath){
                jobPath = jobName;
                jobRoleSkill = _CROF.getSkillOfJobPath(jobPath);
            }else{
                jobRoleSkill = _CROF.getSkillOfJobPath(jobPath);
                jobRoleSkill = jobRoleSkill.concat(_CROF.getSkillOfJob(jobName));
            }

            var tmpnpc = {
                name:npcname,face:face,pf:pf,ftype:ftype,job:jobName,
                plv:isRandom?Math.floor(attr*Math.random()):attr,
                dlv:isRandom?Math.floor(attr*Math.random()):attr,
                ilv:isRandom?Math.floor(attr*Math.random()):attr,
                mlv:isRandom?Math.floor(attr*Math.random()):attr,
                hlv:isRandom?Math.floor(attr*Math.random()):attr,
                skill:jobRoleSkill,
                xw:_CU.cloneObj(jobConfig.xw),
                equip:[],
            };

            if(!tmpnpc.xw[ftypexw]){

                var ftypexwvalue = 1;
                if(jobConfig&&jobConfig.level){
                    if(jobConfig.level==1){
                        ftypexwvalue=50;
                    }else if(jobConfig.level==2){
                        ftypexwvalue=100;
                    }else if(jobConfig.level==3){
                        ftypexwvalue=200;
                    }else if(jobConfig.level==4){
                        ftypexwvalue=400;
                    }else if(jobConfig.level==5){
                        ftypexwvalue=800;
                    }
                }
                tmpnpc.xw[ftypexw]=ftypexwvalue;
                //tmpnpc.xw[ftypexw]=1;
            }
            //根据层数上涨修为
            if(chapterLv){
                for(var key in tmpnpc.xw){
                    if(tmpnpc.xw[key]<chapterLv){
                        tmpnpc.xw[key]=chapterLv;
                    }
                }
            }

            //根据修为补充技能
            _CROF.getSkillByXw(tmpnpc,tmpnpc.skill,false,false);

            //2级通配技能
            if(
                hardConf.cskill
                &&chapter
                &&_SKILLBD[chapter+"守军"]
            ){
                tmpnpc.skill.push(chapter+"守军");
            }
           
            //根据职业和实际修为获取装备
            var equipArr = _CROF.getEquipByJobAndRole(jobName,tmpnpc);
            if(equipArr&&equipArr.length>0){
                for(var i=0;i<equipArr.length;i++){
                    tmpnpc.equip.push(_CROF.createEquipByTypeAndDz(equipArr[i],dz));
                }
            }

            tmpnpc.exp = _EXPMANAGER.getExpByLevel(lv);

            var npcdata = _ROLEDATA.getRoleDataOfPlayer(tmpnpc);
            tmpnpc.hp = npcdata.maxHp;
            tmpnpc.mg = npcdata.maxMagic;

            return tmpnpc;
        }

        return null;
    },

    //根据职业获取技能
    getSkillOfJob:function(job){

        if(!_CROF.jobSkillMap[job]){

            var jobskill = [];
            for(var key in _SKILLBD){

                if(_SKILLBD[key].exc&&_CU.isContain(_SKILLBD[key].exc,job)){
                    jobskill.push(key);
                }

            }
            _CROF.jobSkillMap[job]=jobskill;
        }

        return _CU.cloneObj(_CROF.jobSkillMap[job]);
    },

    //根据转职路线获取技能数组
    getSkillOfJobPath:function(target){

        if(!_CROF.jobPathSkillMap){
            _CROF.jobPathSkillMap=[];
            for(var job in _JOBPATH){
                if(_JOBPATH[job]){
                    var temparr = _CU.cloneObj(_JOBPATH[job]);
                    if(_JOB[job])
                        temparr.push(job);
                    var tempskill = [];
                    for(var i=0;i<temparr.length;i++){
                        for(var j=0;j<tempskill.length;j++){
                            var lvupskill = _MJOB.getNextSkillBd(tempskill[j]);
                            if(lvupskill){
                                tempskill[j]=lvupskill;
                            }
                        }

                        tempskill =  tempskill.concat(_CROF.getSkillOfJob(temparr[i]));
                    }
                    _CROF.jobPathSkillMap[job]=tempskill;
                }else{
                    _CROF.jobPathSkillMap[job]=_CROF.getSkillOfJob(job);
                }
            }
        }

        if(_CROF.jobPathSkillMap[target])
            return _CU.cloneObj(_CROF.jobPathSkillMap[target]);

        return [];
    },

    //根据修为获取技能
    getSkillByXw:function(role,skillArr,hasBoss,isjg){
        var maxskillnmb = _SKILLHEPLER.getRoleSkillNmb(role);
        if(skillArr&&skillArr.length>=maxskillnmb){
            return;
        }

        var tempMap = {};
        var tempArr = [];
        var hasMainSkill = false;

        for(var i=0;i<skillArr.length;i++){
            tempMap[skillArr[i]]=1;
            if(_SKILL[skillArr[i]]){
                hasMainSkill = true;
            }
        }

        if(!hasMainSkill){

            if(isjg){
                for(var key in _SKILL){
                    if(
                        !tempMap[key]
                        &&(
                            !_SKILL[key].bossflag
                            ||hasBoss
                        )
                        &&XWH.isXwEnough(_SKILL[key],role)
                    ){
                        tempArr.push(key);
                    }
                }

                tempArr.sort((a,b)=>{
                    var maxa = 0;
                    var maxb = 0;
                    var skilla = _SKILL[a];
                    var skillb = _SKILL[b]
                    for(var txw in skilla.xw){
                        if(skilla.xw[txw]>maxa){
                            maxa = skilla.xw[txw];
                        }
                    }

                    for(var txw in skillb.xw){
                        if(skillb.xw[txw]>maxb){
                            maxb = skillb.xw[txw];
                        }
                    }

                    return maxb-maxa;
                });

                if(tempArr.length>0){
                    skillArr.push(tempArr[0]);
                    tempArr = [];
                }
            }else{

                var tempjmskillarr = _JMS[role.job];
                if(tempjmskillarr&&tempjmskillarr.length>0){

                    for(var i=0;i<tempjmskillarr.length;i++){

                        if(skillArr.length<maxskillnmb){
                            skillArr.push(tempjmskillarr[i]);
                        }

                    }

                }

            }
        }



        for(var key in _SKILLBD){
            if(
                !tempMap[key]
                &&!_SKILLBD[key].exc
                &&(
                    !_SKILLBD[key].bossflag
                    ||hasBoss
                )
                &&XWH.isXwEnough(_SKILLBD[key],role)
                &&skillArr.length<maxskillnmb
            ){
                tempArr.push(key);
            }
        }

        tempArr.sort((a,b)=>{
            var maxa = 0;
            var maxb = 0;
            var skilla = _SKILLBD[a];
            var skillb = _SKILLBD[b]
            for(var txw in skilla.xw){
                if(skilla.xw[txw]>maxa){
                    maxa = skilla.xw[txw];
                }
            }

            for(var txw in skillb.xw){
                if(skillb.xw[txw]>maxb){
                    maxb = skillb.xw[txw];
                }
            }

            return maxb-maxa;
        });

        for(var i=0;i<tempArr.length;i++){
            if(skillArr.length>=maxskillnmb){
                break;
            }else{
                skillArr.push(tempArr[i]);
            }
        }

    }

}